//
//  LogInViewController.m
//  LvBao
//
//  Created by 尹宁 on 16/6/7.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "LogInViewController.h"
#import "UIViewController+HUD.h"
#import "UserMangerDefaults.h"
#import "ForgetPasswordViewController.h"
@interface LogInViewController ()

@end

@implementation LogInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"登录";
    
    [self setCornerRadius:self.CorRadioPasswordView];
    [self setCornerRadius:self.CorRadioPhoneNumberView];
    [self setCornerRadius:self.LogInButton];
    self.LogInButton.layer.borderWidth = 0;
}
- (IBAction)login:(id)sender {
    if (self.InputPhoneNumberTextfield.text.length == 0) {
        [self showHint:@"请输入手机号码" yOffset:-300];
        return;
    }
    if (self.InputPasswordTextfield.text.length == 0) {
        [self showHint:@"请输入密码" yOffset:-300];
        return;
    }
    
    NSDictionary *params = @{@"user_name":self.InputPhoneNumberTextfield.text,@"user_password":self.InputPasswordTextfield.text,@"app_type":@"ios"};
    
    YTKNetHandle *api = [[YTKNetHandle alloc] initWithUrl:@"/Auth/login.html" paramDic:params RegisterApiMethod:RequestMethodPost];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSString *error = [request.responseBody objectForKey:@"error"];
        if ([error isEqualToString:@"0"]) {
            [self showHint:@"登陆成功" yOffset:-300];
            
            //本地化
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setBool:YES forKey:@"is_logIn"];
            NSString *token = [[request.responseBody objectForKey:@"result"] objectForKey:@"token"];
            [userDefaults setObject:token forKey:@"gettoken"];
            [userDefaults setObject:request.responseBody[@"result"][@"imtoken"] forKey:@"imToken"];
            [userDefaults setObject:request.responseBody[@"result"][@"imid"] forKey:@"imId"];
            
            
            [UserMangerDefaults saveUserName:self.InputPhoneNumberTextfield.text password:self.InputPasswordTextfield.text];
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:@"NSNotificationCenter_LOGIN" object:nil];
            [userDefaults synchronize];
            
           
        } else {
            NSString *faultStr = [request.responseBody objectForKey:@"msg"];
            [self showHint:faultStr yOffset:-300];
        }
    } failure:^(YTKBaseRequest *request) {
        
    }];
    [_InputPasswordTextfield resignFirstResponder];
}
- (IBAction)forgetPassword:(id)sender {
    ForgetPasswordViewController *forgotPasswordVC = [[ForgetPasswordViewController alloc] initWithNibName:NSStringFromClass([ForgetPasswordViewController class]) bundle:nil];
    [self.navigationController pushViewController:forgotPasswordVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

- (void)setCornerRadius:(UIView *)view
{
    view.layer.cornerRadius = 20;
    view.layer.masksToBounds = YES;
    view.layer.borderWidth = 1;
    view.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor grayColor]);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
